package misc;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import com.sun.net.httpserver.HttpExchange;

/**
 * @author Filip Lindqvist <buggfille@gmail.com>
 * @created 10:24:06 AM - Nov 16, 2011
 */
public class GifDataStream extends HttpStream {

    public GifDataStream(HttpExchange t) {
        this(t, HTTP.OK);
    }

    public GifDataStream(HttpExchange t, int code) {
        super(t, code);
        super.exchange.getResponseHeaders().add("Content-Type", "image/gif");
        // super.exchange.getResponseHeaders().add("Content-Transfer-Encoding",
        // "8-bit");
    }

    public void write(File f) throws IOException {
        FileInputStream is = new FileInputStream(f);
        try {
            byte[] buffer = new byte[1048576];
            long s = System.currentTimeMillis();
            while (is.read(buffer) > 0) {
                write(new String(buffer, "utf-8"));
                // System.out.println(new String(buffer));
            }
            long e = System.currentTimeMillis();
            System.out.println("gif: " + f.getName() + " 2binary took "
                    + (e - s) + " ms");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
